    program mc1d
    use util_fdm
    use util_cmfd
    use distlog
    use rng
    use HMGZ
    use util_cmfd_eddington, only : prepare_cmfd_EDT
    use nem
    use util_mc
    use mpower
    use wwnd
!    use mvwnd
    implicit none
    include "accm.FI"
    include "pspec.FI"
    include "xsec.FI"
    include "files.FI"
    include "arrays.FI"
    include "hmgz.FI"
    real(8) :: k_cmfd, err
    integer :: iter, i, g
    character*132 mesg
    real(8) :: initk, cmfdk, maxf
    
    open(123, file='test.txt', status='unknown')
    close(123)
    
!    real(8) :: rngtest
!    integer(4) ::i
    
!    do i=1, 1000    
!!        rngtest=GetRN()
!        write(*,*) GetRN()
!    end do

!    ! test moving window
!    real(8), pointer, dimension(:,:) :: t1, t2, o1,o2
!    integer :: i
!    allocate(t1(3,1),o1(3,1))
!    allocate(t2(6,1),o2(6,1))
!    call init_mvwnd(3, 3, 6, 1)
!    t1(1,1) = 0.1_8
!    t1(2,1) = 0.2_8
!    t1(3,1) = 0.3_8
!    t2(1,1) = 0.1_8
!    t2(2,1) = 0.2_8
!    t2(3,1) = 0.3_8
!    t2(4,1) = 0.4_8
!    t2(5,1) = 0.5_8
!    t2(6,1) = 0.6_8
!    do i=1, 4
!        t1 = t1 * i
!        t2 = t2 * i
!        call add_mvwnd(t1,t2)
!        call get_mvwnd(o1,o2)
!        write(*,*) o1
!        write(*,*) o2
!    end do
!    
!    ! end test moving window
    
    
    open(hfiter, file = 'iter.txt', status = 'unknown')
    
    initk =1.
    call readinput
    
    call init_mpower(totmesh)
    
    call init_nem


    call prepare_fdm
    call prepare_cmfd
!    call fdm(initk)
    
    
!    call init_wwnd(totmesh, ng)
!    call update_ww(phi_f)
!    call makeCurrent
!    hk = initk
!    gk = initk
!    phifAC = phi_f
!    jfAC = jf
!    call homogenize_acc
!    call solve_nem
!    call solve_nem4
!    call solve_nem_df
!    call solve_nem_df4    
    
    maxf = 0.
    do i=1, totmesh
        if (maxf<phi_f(i,1)) then
            mpeak=i
            maxf=phi_f(i,1)
        endif 
    end do
    
    
    cmfdk = 1.
    call homogenize_cycle
    call prepare_cmfd
    call prepare_cmfd_EDT
    call fdm_coarsemesh(cmfdk)


!    call solve_nem
!    call solve_nem4
!    
!    phifAC = phi_f
!    call makeCurrent
!    jfAC = jf
!    call cmfd(initk, err, iter)
!    call init_distlog
!    call write_distlog2
    

    
!    call cmfd(k_cmfd, err, iter)
!    


    call MC(initk)
    
    close(hfiter)
    
#ifdef _NOLOG
    do g=1, ng
        do i=1, totmesh
            psi_f(i) = psi_f(i) + Xnuf(complist(i), g) * phifac(i, g) * h(i)
        enddo
    enddo
    phifac = phifac/sum(psi_f)
    open(9999, file = 'trkt.txt', status='unknown')
    write(9999, '(2000(e11.5,1x))') (phifac(i, 1),i=1,totmesh)
    close(9999)
#endif
    end program
    